--POUR CONTROLE


define erp='erp_mli2.';
define outils='outils_mli2.';

DROP TABLE se_gam_poste;

CREATE TABLE se_gam_poste AS (SELECT * FROM &erp.gam_poste WHERE 1=2);






 INSERT INTO se_gam_poste

    (SELECT section section_cod,
            '0' ilot_cod,
            poste poste_cod,
            CENTRECHAR ccmach_cod,
            NULL ccmo_cod,
            '9099' unfab_cod,
            0 enreg_id,
            'N' enreg_s,
            libelle poste_lib,
            NULL poste_lib2,
            'N' sous_traitance_ok,
            'N' chiffragemach_ok,
            NULL qterebmach_coef,
            NULL qterebmo_coef,
            0 alea_coef,
            0 manu_coef, 1 capamach_coef, 1 chargemach_coef, 1 operateur_nb, 1 capamo_coef, 1 chargemo_coef, 1 tpsprepamach_coef, 1 tpsprepamo_coef, 1 tpsreamach_coef, 1 tpsreamo_coef,
            0 chgtps_typ, 'N' ouvbt_ok, 'N' ctrlphaseprec_typ

        FROM uv_ppostes s
        ) ;


 UPDATE se_gam_poste tf SET enreg_id=(select enreg_id FROM &erp.gam_poste f WHERE f.section_cod=tf.section_cod AND f.ilot_cod=tf.ilot_cod AND f.poste_cod=tf.poste_cod) ;
 UPDATE se_gam_poste tf SET enreg_id=&outils.enreg_id.NEXTVAL WHERE enreg_id IS NULL OR enreg_id =0;



 CALL uvse_synchroverserp('section_cod@ilot_cod@poste_cod','GAM_POSTE','O') ;

 commit;
 
 
select * from (
SELECT &outils.stragg(source) source,section_cod, ilot_cod, poste_cod, ccmach_cod, ccmo_cod, unfab_cod, enreg_id, enreg_s, poste_lib, poste_lib2, sous_traitance_ok, chiffragemach_ok, qterebmach_coef, qterebmo_coef, alea_coef, manu_coef, capamach_coef, chargemach_coef, operateur_nb, capamo_coef, chargemo_coef, tpsprepamach_coef, tpsprepamo_coef, tpsreamach_coef, tpsreamo_coef, chgtps_typ, ouvbt_ok, ctrlphaseprec_typ
FROM (
  SELECT 'uv' source ,g.* FROM se_gam_poste g
  UNION ALL
  SELECT 'se' source ,g.* FROM &erp.gam_poste g

  )
GROUP BY section_cod, ilot_cod, poste_cod, ccmach_cod, ccmo_cod, unfab_cod, enreg_id, enreg_s, poste_lib, poste_lib2, sous_traitance_ok, chiffragemach_ok, qterebmach_coef, qterebmo_coef, alea_coef, manu_coef, capamach_coef, chargemach_coef, operateur_nb, capamo_coef, chargemo_coef, tpsprepamach_coef, tpsprepamo_coef, tpsreamach_coef, tpsreamo_coef, chgtps_typ, ouvbt_ok, ctrlphaseprec_typ
HAVING Count(1)!=2) r
ORDER BY  section_cod,poste_cod